package it.cums.ui.util;

import it.cums.core.model.Customer;
import it.cums.core.model.Order;
import it.cums.core.model.Product;
import it.cums.core.model.Supplier;
import it.cums.ui.Activator;
import it.cums.ui.editors.customer.CustomerEditorInput;
import it.cums.ui.editors.order.OrderEditorInput;
import it.cums.ui.editors.product.ProductEditorInput;
import it.cums.ui.editors.supplier.SupplierEditorInput;

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.PlatformUI;

public class EditorUtils {
	
	public static void closeEditors(Object obj) {
		if (obj instanceof Customer) {
			closeEditors((Customer)obj);
		} else if (obj instanceof Order) {
			closeEditors((Order)obj);
		} else if (obj instanceof Product) {
			closeEditors((Product)obj);
		} else if (obj instanceof Supplier) {
			closeEditors((Supplier)obj);
		}
	}

	private static void closeEditors(Customer customer) {
		try {
			for (IEditorReference reference : PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()) {
				IEditorPart editorPart = reference.getEditor(false);
				IEditorInput input = reference.getEditorInput();
				if (input instanceof CustomerEditorInput) {
					CustomerEditorInput customerInput = (CustomerEditorInput)input;
					if (customerInput.getCustomer().getId() == customer.getId()) {
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeEditor(editorPart, false);
					}
				} else if (input instanceof OrderEditorInput) {
					OrderEditorInput orderEditorInput = (OrderEditorInput)input;
					for (Order order : customer.getOrders().getOrders()) {
						if (orderEditorInput.getOrder().getId() == order.getId()) {
							PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeEditor(editorPart, false);
						}
					}
				}
			}
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
		}
	}
	
	private static void closeEditors(Order order) {
		try {
			for (IEditorReference reference : PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()) {
				IEditorPart editorPart = reference.getEditor(false);
				IEditorInput input = reference.getEditorInput();
				if (input instanceof OrderEditorInput) {
					OrderEditorInput orderInput = (OrderEditorInput)input;
					if (orderInput.getOrder().getId() == order.getId()) {
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeEditor(editorPart, false);
					}
				}
			}
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
		}
	}
	
	private static void closeEditors(Product product) {
		try {
			for (IEditorReference reference : PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()) {
				IEditorPart editorPart = reference.getEditor(false);
				IEditorInput input = reference.getEditorInput();
				if (input instanceof ProductEditorInput) {
					ProductEditorInput productInput = (ProductEditorInput)input;
					if (productInput.getProduct().getId() == product.getId()) {
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeEditor(editorPart, false);
					}
				}
			}
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
		}
	}
	
	private static void closeEditors(Supplier supplier) {
		try {
			for (IEditorReference reference : PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()) {
				IEditorPart editorPart = reference.getEditor(false);
				IEditorInput input = reference.getEditorInput();
				if (input instanceof SupplierEditorInput) {
					SupplierEditorInput supplierInput = (SupplierEditorInput)input;
					if (supplierInput.getSupplier().getId() == supplier.getId()) {
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeEditor(editorPart, false);
					}
				}
			}
		} catch (Exception e) {
			Activator.getDefault().logError(e.getMessage(), e);
		}
	}
}
